package org.bonorsoft.common.dingapi;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiCallBackDeleteCallBackRequest;
import com.dingtalk.api.request.OapiCallBackRegisterCallBackRequest;
import com.dingtalk.api.response.OapiCallBackRegisterCallBackResponse;
import org.bonorsoft.common.constant.CommonConstant;
import org.bonorsoft.common.constant.EducationConstant;
import org.bonorsoft.common.constant.EducationConstant;
import org.bonorsoft.common.constant.URLConstant;

import java.util.Arrays;

public class DingdingCallback {

    public static void main(String[] args) throws Exception{
        // 先删除企业已有的回调
        DingTalkClient client = new DefaultDingTalkClient(URLConstant.DELETE_CALLBACK);
        OapiCallBackDeleteCallBackRequest request = new OapiCallBackDeleteCallBackRequest();
        request.setHttpMethod("GET");
        client.execute(request, AccessTokenUtil.getToken());

        // 重新为企业注册回调
        client = new DefaultDingTalkClient(URLConstant.REGISTER_CALLBACK);
        OapiCallBackRegisterCallBackRequest registerRequest = new OapiCallBackRegisterCallBackRequest();
        registerRequest.setUrl(EducationConstant.CALLBACK_URL_HOST + "/callback");
        registerRequest.setAesKey(EducationConstant.ENCODING_AES_KEY);
        registerRequest.setToken(EducationConstant.TOKEN);
        registerRequest.setCallBackTag(Arrays.asList(CommonConstant.ORG_DEPT_CREAT,
                CommonConstant.ORG_DEPT_MODIFY,
                CommonConstant.ORG_DEPT_REMOVE,
                CommonConstant.USER_ADD_ORG,
                CommonConstant.USER_MODIFY_ORG,
                CommonConstant.USER_LEAVE_ORG,
                CommonConstant.USER_ACTIVE_ORG,
                CommonConstant.ORG_ADMIN_ADD,
                CommonConstant.ORG_ADMIN_REMOVE,
                CommonConstant.LABEL_USER_CHANGE,
                CommonConstant.LABEL_CONF_ADD,
                CommonConstant.LABEL_CONF_DEL,
                CommonConstant.LABEL_CONF_MODIFY,
                CommonConstant.ORG_CHANGE,
                CommonConstant.ORG_REMOVE));
        OapiCallBackRegisterCallBackResponse registerResponse = client.execute(registerRequest,AccessTokenUtil.getToken());
        if (registerResponse.isSuccess()) {
            System.out.println("回调注册成功了！！！");
        }
    }

}
